<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>spat3d</title>
    <link rel="stylesheet" type="text/css" href="csound.css" />
    <meta name="generator" content="DocBook XSL Stylesheets V1.78.1" />
    <link rel="home" href="index.html" title="The Canonical Csound Reference Manual" />
    <link rel="up" href="OpcodesTop.html" title="Orchestra Opcodes and Operators" />
    <link rel="prev" href="space.html" title="space" />
    <link rel="next" href="spat3di.html" title="spat3di" />
  </head>
  <body>
    <div class="navheader">
      <table width="100%" summary="Navigation header">
        <tr>
          <th colspan="3" align="center">spat3d</th>
        </tr>
        <tr>
          <td width="20%" align="left"><a accesskey="p" href="space.html">Prev</a> </td>
          <th width="60%" align="center">Orchestra Opcodes and Operators</th>
          <td width="20%" align="right"> <a accesskey="n" href="spat3di.html">Next</a></td>
        </tr>
      </table>
      <hr />
    </div>
    <div class="refentry">
      <a id="spat3d"></a>
      <div class="titlepage"></div>
      <a id="IndexSpat3d" class="indexterm"></a>
      <div class="refnamediv">
        <h2>
          <span class="refentrytitle">spat3d</span>
        </h2>
        <p>spat3d — 
      Positions the input sound in a 3D space and allows moving the sound at k-rate.
          </p>
      </div>
      <div class="refsect1">
        <a id="idp149959504"></a>
        <h2>Description</h2>
        <p>
      This opcode positions the input sound in a 3D space, with optional simulation of room acoustics, in various output formats. <span class="emphasis"><em>spat3d</em></span> allows moving the sound at k-rate (this movement is interpolated internally to eliminate "zipper noise" if sr not equal to kr).
    </p>
      </div>
      <div class="refsect1">
        <a id="idp149961568"></a>
        <h2>Syntax</h2>
        <pre class="synopsis">aW, aX, aY, aZ <span class="command"><strong>spat3d</strong></span> ain, kX, kY, kZ, idist, ift, imode, imdel, iovr [, istor]</pre>
      </div>
      <div class="refsect1">
        <a id="idp149963776"></a>
        <h2>Initialization</h2>
        <p>
      <span class="emphasis"><em>idist</em></span> -- For modes 0 to 3, <span class="emphasis"><em>idist</em></span> is the unit circle distance in meters. For mode 4, <span class="emphasis"><em>idist</em></span> is the distance between microphones. 
    </p>
        <p>
      The following formulas describe amplitude and delay as a function of sound source distance from microphone(s):
      </p>
        <div class="literallayout">
          <p><br />
amplitude = 1 / (0.1 + distance)<br />
      </p>
        </div>
        <p>

      </p>
        <div class="literallayout">
          <p><br />
delay = distance / 340 (in seconds)<br />
      </p>
        </div>
        <p>
    </p>
        <p>
      Distance can be calculated as:

      </p>
        <div class="literallayout">
          <p><br />
distance = sqrt(iX^2 + iY^2 + iZ^2)<br />
      </p>
        </div>
        <p>

      In Mode 4, distance can be calculated as:

      </p>
        <div class="literallayout">
          <p><br />
distance from left mic = sqrt((iX + idist/2)^2 + iY^2 + iZ^2)<br />
distance from right mic = sqrt((iX - idist/2)^2 + iY^2 + iZ^2)<br />
      </p>
        </div>
        <p>
    </p>
        <p>
      With <span class="emphasis"><em>spat3d</em></span> the distance between the sound source and any microphone should be at least (340 * 18) / sr meters. Shorter distances will work, but may produce artifacts in some cases.  There is no such limitation for <span class="emphasis"><em>spat3di</em></span> and <span class="emphasis"><em>spat3dt</em></span>.
    </p>
        <p>
      Sudden changes or discontinuities in sound source location can result in pops or clicks. Very fast movement may also degrade quality.
    </p>
        <p>
      <span class="emphasis"><em>ift</em></span> -- Function table storing room parameters (for free field spatialization, set it to zero or negative). Table size is 54. The values in the table are:

      </p>
        <div class="informaltable">
          <table border="1">
            <colgroup>
              <col />
              <col />
            </colgroup>
            <thead>
              <tr>
                <th>Room Parameter</th>
                <th>Purpose</th>
              </tr>
            </thead>
            <tbody>
              <tr>
                <td>0</td>
                <td>Early reflection recursion depth (0 is the sound source, 1 is the first reflection etc.) for spat3d and spat3di. The number of echoes for four walls (front, back, right, left) is: N = (2*R + 2) * R. If all six walls are enabled: N = (((4*R + 6)*R + 8)*R) / 3</td>
              </tr>
              <tr>
                <td>1</td>
                <td>Late reflection recursion depth (used by spat3dt only). spat3dt skips early reflections and renders echoes up to this level. If early reflection depth is negative, spat3d and spat3di will output zero, while spat3dt will start rendering from the sound source.</td>
              </tr>
              <tr>
                <td>2</td>
                <td>imdel for spat3d. Overrides opcode parameter if non-negative.</td>
              </tr>
              <tr>
                <td>3</td>
                <td>irlen for spat3dt. Overrides opcode parameter if non-negative.</td>
              </tr>
              <tr>
                <td>4</td>
                <td>idist value. Overrides opcode parameter if &gt;= 0.</td>
              </tr>
              <tr>
                <td>5</td>
                <td>Random seed (0 - 65535) -1 seeds from current time.</td>
              </tr>
              <tr>
                <td>6 - 53</td>
                <td>wall parameters (w = 6: ceil, w = 14: floor, w = 22: front, w = 30: back, w = 38: right, w = 46: left)</td>
              </tr>
              <tr>
                <td>w + 0</td>
                <td>Enable reflections from this wall (0: no, 1: yes)</td>
              </tr>
              <tr>
                <td>w + 1</td>
                <td>Wall distance from listener (in meters)</td>
              </tr>
              <tr>
                <td>w + 2</td>
                <td>Randomization of wall distance (0 - 1) (in units of 1 / (wall distance))</td>
              </tr>
              <tr>
                <td>w + 3</td>
                <td>Reflection level (-1 - 1)</td>
              </tr>
              <tr>
                <td>w + 4</td>
                <td>Parametric equalizer frequency in Hz.</td>
              </tr>
              <tr>
                <td>w + 5</td>
                <td>Parametric equalizer level (1.0: no filtering)</td>
              </tr>
              <tr>
                <td>w + 6</td>
                <td>Parametric equalizer Q (0.7071: no resonance)</td>
              </tr>
              <tr>
                <td>w + 7</td>
                <td>Parametric equalizer mode (0: peak EQ, 1: low shelf, 2: high shelf)</td>
              </tr>
            </tbody>
          </table>
        </div>
        <p>
    </p>
        <p>
      <span class="emphasis"><em>imode</em></span> -- Output mode
      </p>
        <div class="itemizedlist">
          <ul class="itemizedlist" style="list-style-type: disc; ">
            <li class="listitem">
              <p>0: B format with W output only (mono)
</p>
              <div class="literallayout">
                <p><br />
aout    =  aW<br />
</p>
              </div>
              <p>
        </p>
            </li>
            <li class="listitem">
              <p>1: B format with W and Y output (stereo)
</p>
              <div class="literallayout">
                <p><br />
aleft   =  aW + 0.7071*aY<br />
aright  =  aW - 0.7071*aY<br />
</p>
              </div>
              <p>
        </p>
            </li>
            <li class="listitem">
              <p>2: B format with W, X, and Y output (2D). This can be converted to UHJ:
</p>
              <div class="literallayout">
                <p><br />
aWre, aWim      hilbert aW<br />
aXre, aXim      hilbert aX<br />
aYre, aYim      hilbert aY<br />
aWXr    =  0.0928*aXre + 0.4699*aWre<br />
aWXiYr  =  0.2550*aXim - 0.1710*aWim + 0.3277*aYre<br />
aleft   =  aWXr + aWXiYr<br />
aright  =  aWXr - aWXiYr<br />
</p>
              </div>
              <p>
        </p>
            </li>
            <li class="listitem">
              <p>3: B format with all outputs (3D)</p>
            </li>
            <li class="listitem">
              <p>4: Simulates a pair of microphones (stereo output)
</p>
              <div class="literallayout">
                <p><br />
aW      butterlp aW, ifreq      ; recommended values for ifreq<br />
aY      butterlp aY, ifreq      ; are around 1000 Hz<br />
aleft   =  aW + aX<br />
aright  =  aY + aZ<br />
</p>
              </div>
              <p>
        </p>
            </li>
          </ul>
        </div>
        <p>
    </p>
        <p>
      Mode 0 is the cheapest to calculate, while mode 4 is the most expensive.
    </p>
        <p>
      In Mode 4, The optional lowpass filters can change the frequency response depending on direction. For example, if the sound source is located left to the listener then the high frequencies are attenuated in the right channel and slightly increased in the left. This effect can be disabled by not using filters. You can also experiment with other filters (tone etc.) for better effect.
    </p>
        <p>
      Note that mode 4 is most useful for listening with headphones, and is also more expensive to calculate than the B-format (0 to 3) modes. The <span class="emphasis"><em>idist</em></span> parameter in this case sets the distance between left and right microphone; for headphones, values between 0.2 - 0.25 are recommended, although higher settings up to 0.4 may be used for wide stereo effects.
    </p>
        <p>
      More information about B format can be found here: <a class="ulink" href="http://www.york.ac.uk/inst/mustech/3d_audio/ambis2.htm" target="_top"><em class="citetitle">http://www.york.ac.uk/inst/mustech/3d_audio/ambis2.htm</em></a>
    </p>
        <p>
      <span class="emphasis"><em>imdel</em></span> -- Maximum delay time for spat3d in seconds. This has to be longer than the delay time of the latest reflection (depends on room dimensions, sound source distance, and recursion depth; using this formula gives a safe (although somewhat overestimated) value:

       </p>
        <div class="literallayout">
          <p><br />
imdel = (R + 1) * sqrt(W*W + H*H + D*D) / 340.0<br />
       </p>
        </div>
        <p>

      where R is the recursion depth, W, H, and D are the width, height, and depth
  of the room, respectively).
    </p>
        <p>
      <span class="emphasis"><em>iovr</em></span> -- Oversample ratio for spat3d (1 to 8). Setting it higher improves quality at the expense of memory and CPU usage. The recommended value is 2.
    </p>
        <p>
      <span class="emphasis"><em>istor</em></span> (optional, default=0) -- Skip initialization if non-zero (default: 0).
    </p>
      </div>
      <div class="refsect1">
        <a id="idp150017408"></a>
        <h2>Performance</h2>
        <p>
      <span class="emphasis"><em>aW, aX, aY, aZ</em></span> -- Output signals

      </p>
        <div class="informaltable">
          <table border="1">
            <colgroup>
              <col />
              <col />
              <col />
              <col />
              <col />
              <col />
            </colgroup>
            <thead>
              <tr>
                <th> </th>
                <th>mode 0</th>
                <th>mode 1</th>
                <th>mode 2</th>
                <th>mode 3</th>
                <th>mode 4</th>
              </tr>
            </thead>
            <tbody>
              <tr>
                <td>aW</td>
                <td>W out</td>
                <td>W out</td>
                <td>W out</td>
                <td>W out</td>
                <td>left chn / low freq.</td>
              </tr>
              <tr>
                <td>aX</td>
                <td>0</td>
                <td>0</td>
                <td>X out</td>
                <td>X out</td>
                <td>left chn / high frq.</td>
              </tr>
              <tr>
                <td>aY</td>
                <td>0</td>
                <td>Y out</td>
                <td>Y out</td>
                <td>Y out</td>
                <td>right chn / low frq.</td>
              </tr>
              <tr>
                <td>aZ</td>
                <td>0</td>
                <td>0</td>
                <td>0</td>
                <td>Z out</td>
                <td>right chn / high fr.</td>
              </tr>
            </tbody>
          </table>
        </div>
        <p>
    </p>
        <p>
      <span class="emphasis"><em>ain</em></span> -- Input signal
    </p>
        <p>
      <span class="emphasis"><em>kX, kY, kZ</em></span> -- Sound source coordinates (in meters)
    </p>
        <p>
      If you encounter very slow performance (up to 100 times slower), it may be caused by denormals (this is also true of many other IIR opcodes, including <a class="link" href="butterlp.html" title="butterlp"><em class="citetitle">butterlp</em></a>, <a class="link" href="pareq.html" title="pareq"><em class="citetitle">pareq</em></a>, <a class="link" href="hilbert.html" title="hilbert"><em class="citetitle">hilbert</em></a>, and many others). Underflows can be avoided by:
      </p>
        <div class="itemizedlist">
          <ul class="itemizedlist" style="list-style-type: disc; ">
            <li class="listitem">
              <p>Using the <a class="link" href="denorm.html" title="denorm"><em class="citetitle">denorm</em></a> opcode on <span class="emphasis"><em>ain</em></span> before <span class="emphasis"><em>spat3d</em></span>.</p>
            </li>
            <li class="listitem">
              <p>mixing low level DC or noise to the input signal, e.g. </p>
              <div class="literallayout">
                <p>atmp rnd31 1/1e24, 0, 0</p>
              </div>
              <p> </p>
              <div class="literallayout">
                <p>aW, aX, aY, aZ spa3di ain + atmp, ...</p>
              </div>
              <p> or </p>
              <div class="literallayout">
                <p>aW, aX, aY, aZ spa3di ain + 1/1e24, ...</p>
              </div>
            </li>
            <li class="listitem">
              <p>reducing <span class="emphasis"><em>irlen</em></span> in the case of <span class="emphasis"><em>spat3dt</em></span> (which does not have an input signal). A value of about 0.005 is suitable for most uses, although it also depends on EQ settings. If the equalizer is not used, <span class="quote">“<span class="quote">irlen</span>”</span> can be set to 0.</p>
            </li>
          </ul>
        </div>
        <p>
    </p>
      </div>
      <div class="refsect1">
        <a id="idp150049488"></a>
        <h2>Examples</h2>
        <p>
      Here is a example of the spat3d opcode that outputs a stereo file. It uses the file <a class="ulink" href="examples/spat3d_stereo.csd" target="_top"><em class="citetitle">spat3d_stereo.csd</em></a>.

      </p>
        <div class="example">
          <a id="idp150051392"></a>
          <p class="title">
            <strong>Example 831. Stereo example of the spat3d opcode.</strong>
          </p>
          <div class="example-contents">
            <p>See the sections <a class="link" href="UsingRealTime.html" title="Real-Time Audio"><em class="citetitle">Real-time Audio</em></a> and <a class="link" href="CommandFlags.html" title="Csound command line"><em class="citetitle">Command Line Flags</em></a> for more information on using command line flags.</p>
            <pre class="programlisting">
<span class="csdtag">&lt;CsoundSynthesizer&gt;</span>
<span class="csdtag">&lt;CsOptions&gt;</span>
<span class="comment">; Select audio/midi flags here according to platform</span>
<span class="comment">; Audio out   Audio in    No messages</span>
-odac           -iadc     -d     <span class="comment">;;;RT audio I/O</span>
<span class="comment">; For Non-realtime ouput leave only the line below:</span>
<span class="comment">; -o spat3d_stereo.wav -W ;;; for file output any platform</span>
<span class="csdtag">&lt;/CsOptions&gt;</span>
<span class="csdtag">&lt;CsInstruments&gt;</span>

<span class="op">/</span><span class="op">*</span> Written by Istvan Varga <span class="op">*</span><span class="op">/</span>
<span class="ohdr">sr</span>      <span class="op">=</span>  48000
<span class="ohdr">kr</span>      <span class="op">=</span>  1000
<span class="ohdr">ksmps</span>   <span class="op">=</span>  48
<span class="ohdr">nchnls</span>  <span class="op">=</span>  2

<span class="op">/</span><span class="op">*</span> room parameters <span class="op">*</span><span class="op">/</span>

idep    <span class="op">=</span>  3    <span class="op">/</span><span class="op">*</span> early reflection depth       <span class="op">*</span><span class="op">/</span>

itmp    <span class="ohdr">ftgen</span>   1, 0, 64, <span class="op">-</span>2,                                           \
		<span class="op">/</span><span class="op">*</span> depth1, depth2, <span class="opc">max</span> delay, IR length, idist, <span class="ohdr">seed</span> <span class="op">*</span><span class="op">/</span> \
		idep, 48, <span class="op">-</span>1, 0.01, 0.25, 123,                          \
		1, 21.982, 0.05, 0.87, 4000.0, 0.6, 0.7, 2, <span class="op">/</span><span class="op">*</span> <span class="opc">ceil</span>  <span class="op">*</span><span class="op">/</span> \
		1,  1.753, 0.05, 0.87, 3500.0, 0.5, 0.7, 2, <span class="op">/</span><span class="op">*</span> <span class="opc">floor</span> <span class="op">*</span><span class="op">/</span> \
		1, 15.220, 0.05, 0.87, 5000.0, 0.8, 0.7, 2, <span class="op">/</span><span class="op">*</span> front <span class="op">*</span><span class="op">/</span> \
		1,  9.317, 0.05, 0.87, 5000.0, 0.8, 0.7, 2, <span class="op">/</span><span class="op">*</span> back  <span class="op">*</span><span class="op">/</span> \
		1, 17.545, 0.05, 0.87, 5000.0, 0.8, 0.7, 2, <span class="op">/</span><span class="op">*</span> right <span class="op">*</span><span class="op">/</span> \
		1, 12.156, 0.05, 0.87, 5000.0, 0.8, 0.7, 2  <span class="op">/</span><span class="op">*</span> left  <span class="op">*</span><span class="op">/</span>

	<span class="oblock">instr</span> 1

<span class="op">/</span><span class="op">*</span> some source signal <span class="op">*</span><span class="op">/</span>

a1      <span class="opc">phasor</span> 150              <span class="comment">; oscillator</span>
a1      <span class="opc">butterbp</span> a1, 500, 200   <span class="comment">; filter</span>
a1      <span class="op">=</span>  <span class="opc">taninv</span>(a1 <span class="op">*</span> 100)
a2      <span class="opc">phasor</span> 3                <span class="comment">; envelope</span>
a2      mirror 40<span class="op">*</span>a2, <span class="op">-</span>100, 5
a2      limit a2, 0, 1
a1      <span class="op">=</span>  a1 <span class="op">*</span> a2 <span class="op">*</span> 9000

kazim   <span class="opc">line</span> 0, 2.5, 360        <span class="comment">; move sound source around</span>
kdist   <span class="opc">line</span> 1, 10, 4           <span class="comment">; distance</span>

<span class="comment">; convert polar coordinates</span>
kX      <span class="op">=</span>  <span class="opc">sin</span>(kazim <span class="op">*</span> 3.14159 <span class="op">/</span> 180) <span class="op">*</span> kdist
kY      <span class="op">=</span>  <span class="opc">cos</span>(kazim <span class="op">*</span> 3.14159 <span class="op">/</span> 180) <span class="op">*</span> kdist
kZ      <span class="op">=</span>  0

a1      <span class="op">=</span>  a1 <span class="op">+</span> 0.000001 <span class="op">*</span> 0.000001     <span class="comment">; avoid underflows</span>

imode   <span class="op">=</span>  1    <span class="comment">; change this to 3 for 8 spk in a cube,</span>
		<span class="comment">; or 1 for simple stereo</span>

aW, aX, aY, aZ  <span class="opc">spat3d</span> a1, kX, kY, kZ, 1.0, 1, imode, 2, 2

aW      <span class="op">=</span>  aW <span class="op">*</span> 1.4142

<span class="comment">; stereo</span>
<span class="comment">;</span>
aL     <span class="op">=</span>  aW <span class="op">+</span> aY              <span class="op">/</span><span class="op">*</span> left                 <span class="op">*</span><span class="op">/</span>
aR     <span class="op">=</span>  aW <span class="op">-</span> aY              <span class="op">/</span><span class="op">*</span> right                <span class="op">*</span><span class="op">/</span>

<span class="comment">; quad (square)</span>
<span class="comment">;</span>
<span class="comment">;aFL     =  aW + aX + aY         /* front left           */</span>
<span class="comment">;aFR     =  aW + aX - aY         /* front right          */</span>
<span class="comment">;aRL     =  aW - aX + aY         /* rear left            */</span>
<span class="comment">;aRR     =  aW - aX - aY         /* rear right           */</span>

<span class="comment">; eight channels (cube)</span>
<span class="comment">;</span>
<span class="comment">;aUFL   =  aW + aX + aY + aZ    /* upper front left     */</span>
<span class="comment">;aUFR   =  aW + aX - aY + aZ    /* upper front right    */</span>
<span class="comment">;aURL   =  aW - aX + aY + aZ    /* upper rear left      */</span>
<span class="comment">;aURR   =  aW - aX - aY + aZ    /* upper rear right     */</span>
<span class="comment">;aLFL   =  aW + aX + aY - aZ    /* lower front left     */</span>
<span class="comment">;aLFR   =  aW + aX - aY - aZ    /* lower front right    */</span>
<span class="comment">;aLRL   =  aW - aX + aY - aZ    /* lower rear left      */</span>
<span class="comment">;aLRR   =  aW - aX - aY - aZ    /* lower rear right     */</span>

	<span class="opc">outs</span> aL, aR

	<span class="oblock">endin</span>


<span class="csdtag">&lt;/CsInstruments&gt;</span>
<span class="csdtag">&lt;CsScore&gt;</span>

/* Written by Istvan Varga */
<span class="stamnt">i</span> 1 0 10
<span class="stamnt">e</span>


<span class="csdtag">&lt;/CsScore&gt;</span>
<span class="csdtag">&lt;/CsoundSynthesizer&gt;</span>
</pre>
          </div>
        </div>
        <p><br class="example-break" />
    </p>
        <p>
      Here is a example of the spat3d opcode that outputs a UHJ file. It uses the file <a class="ulink" href="examples/spat3d_UHJ.csd" target="_top"><em class="citetitle">spat3d_UHJ.csd</em></a>.

      </p>
        <div class="example">
          <a id="idp150056768"></a>
          <p class="title">
            <strong>Example 832. UHJ example of the spat3d opcode.</strong>
          </p>
          <div class="example-contents">
            <pre class="programlisting">
<span class="csdtag">&lt;CsoundSynthesizer&gt;</span>
<span class="csdtag">&lt;CsOptions&gt;</span>
<span class="comment">; Select audio/midi flags here according to platform</span>
<span class="comment">; Audio out   Audio in    No messages</span>
-odac           -iadc     -d     <span class="comment">;;;RT audio I/O</span>
<span class="comment">; For Non-realtime ouput leave only the line below:</span>
<span class="comment">; -o spat3d_UHJ.wav -W ;;; for file output any platform</span>
<span class="csdtag">&lt;/CsOptions&gt;</span>
<span class="csdtag">&lt;CsInstruments&gt;</span>

<span class="op">/</span><span class="op">*</span> Written by Istvan Varga <span class="op">*</span><span class="op">/</span>
<span class="ohdr">sr</span>	<span class="op">=</span>  48000
<span class="ohdr">kr</span>	<span class="op">=</span>  750
<span class="ohdr">ksmps</span>	<span class="op">=</span>  64
<span class="ohdr">nchnls</span>	<span class="op">=</span>  2

itmp    <span class="ohdr">ftgen</span>   1, 0, 64, <span class="op">-</span>2,                                           \
		<span class="op">/</span><span class="op">*</span> depth1, depth2, <span class="opc">max</span> delay, IR length, idist, <span class="ohdr">seed</span> <span class="op">*</span><span class="op">/</span> \
		3, 48, <span class="op">-</span>1, 0.01, 0.25, 123,				\
		1, 21.982, 0.05, 0.87, 4000.0, 0.6, 0.7, 2, <span class="op">/</span><span class="op">*</span> <span class="opc">ceil</span>  <span class="op">*</span><span class="op">/</span> \
		1,  1.753, 0.05, 0.87, 3500.0, 0.5, 0.7, 2, <span class="op">/</span><span class="op">*</span> <span class="opc">floor</span> <span class="op">*</span><span class="op">/</span> \
		1, 15.220, 0.05, 0.87, 5000.0, 0.8, 0.7, 2, <span class="op">/</span><span class="op">*</span> front <span class="op">*</span><span class="op">/</span> \
		1,  9.317, 0.05, 0.87, 5000.0, 0.8, 0.7, 2, <span class="op">/</span><span class="op">*</span> back  <span class="op">*</span><span class="op">/</span> \
		1, 17.545, 0.05, 0.87, 5000.0, 0.8, 0.7, 2, <span class="op">/</span><span class="op">*</span> right <span class="op">*</span><span class="op">/</span> \
		1, 12.156, 0.05, 0.87, 5000.0, 0.8, 0.7, 2  <span class="op">/</span><span class="op">*</span> left  <span class="op">*</span><span class="op">/</span>

	<span class="oblock">instr</span> 1

p3	<span class="op">=</span>  p3 <span class="op">+</span> 1.0

kazim	<span class="opc">line</span> 0.0, 4.0, 360.0		<span class="comment">; azimuth</span>
kelev	<span class="opc">line</span> 40, p3 <span class="op">-</span> 1.0, <span class="op">-</span>20		<span class="comment">; elevation</span>
kdist	<span class="op">=</span>  2.0				<span class="comment">; distance</span>
<span class="comment">; convert coordinates</span>
kX	<span class="op">=</span>  kdist <span class="op">*</span> <span class="opc">cos</span>(kelev <span class="op">*</span> 0.01745329) <span class="op">*</span> <span class="opc">sin</span>(kazim <span class="op">*</span> 0.01745329)
kY	<span class="op">=</span>  kdist <span class="op">*</span> <span class="opc">cos</span>(kelev <span class="op">*</span> 0.01745329) <span class="op">*</span> <span class="opc">cos</span>(kazim <span class="op">*</span> 0.01745329)
kZ	<span class="op">=</span>  kdist <span class="op">*</span> <span class="opc">sin</span>(kelev <span class="op">*</span> 0.01745329)

<span class="comment">; source signal</span>
a1	<span class="opc">phasor</span> 160.0
a2	<span class="opc">delay1</span> a1
a1	<span class="op">=</span>  a1 <span class="op">-</span> a2
kffrq1	<span class="opc">port</span> 200.0, 0.8, 12000.0
affrq	<span class="opc">upsamp</span> kffrq1
affrq	<span class="opc">pareq</span> affrq, 5.0, 0.0, 1.0, 2
kffrq	<span class="opc">downsamp</span> affrq
aenv4	<span class="opc">phasor</span> 3.0
aenv4	limit 2.0 <span class="op">-</span> aenv4 <span class="op">*</span> 8.0, 0.0, 1.0
a1	<span class="opc">butterbp</span> a1 <span class="op">*</span> aenv4, kffrq, 160.0
aenv	<span class="opc">linseg</span> 1.0, p3 <span class="op">-</span> 1.0, 1.0, 0.04, 0.0, 1.0, 0.0
a_	<span class="op">=</span>  4000000 <span class="op">*</span> a1 <span class="op">*</span> aenv <span class="op">+</span> 0.00000001

<span class="comment">; spatialize</span>
a_W, a_X, a_Y, a_Z	<span class="opc">spat3d</span> a_, kX, kY, kZ, 1.0, 1, 2, 2.0, 2

<span class="comment">; convert to UHJ format (stereo)</span>
aWre, aWim	<span class="opc">hilbert</span> a_W
aXre, aXim	<span class="opc">hilbert</span> a_X
aYre, aYim	<span class="opc">hilbert</span> a_Y

aWXre	<span class="op">=</span>  0.0928<span class="op">*</span>aXre <span class="op">+</span> 0.4699<span class="op">*</span>aWre
aWXim	<span class="op">=</span>  0.2550<span class="op">*</span>aXim <span class="op">-</span> 0.1710<span class="op">*</span>aWim

aL	<span class="op">=</span>  aWXre <span class="op">+</span> aWXim <span class="op">+</span> 0.3277<span class="op">*</span>aYre
aR	<span class="op">=</span>  aWXre <span class="op">-</span> aWXim <span class="op">-</span> 0.3277<span class="op">*</span>aYre

	<span class="opc">outs</span> aL, aR

	<span class="oblock">endin</span>


<span class="csdtag">&lt;/CsInstruments&gt;</span>
<span class="csdtag">&lt;CsScore&gt;</span>

/* Written by Istvan Varga */
<span class="stamnt">t</span> 0 60

<span class="stamnt">i</span> 1 0.0 8.0
<span class="stamnt">e</span>


<span class="csdtag">&lt;/CsScore&gt;</span>
<span class="csdtag">&lt;/CsoundSynthesizer&gt;</span>
</pre>
          </div>
        </div>
        <p><br class="example-break" />
    </p>
        <p>
      Here is a example of the spat3d opcode that outputs a quadrophonic file. It uses the file <a class="ulink" href="examples/spat3d_quad.csd" target="_top"><em class="citetitle">spat3d_quad.csd</em></a>.

      </p>
        <div class="example">
          <a id="idp150059792"></a>
          <p class="title">
            <strong>Example 833. Quadrophonic example of the spat3d opcode.</strong>
          </p>
          <div class="example-contents">
            <pre class="programlisting">
<span class="csdtag">&lt;CsoundSynthesizer&gt;</span>
<span class="csdtag">&lt;CsOptions&gt;</span>
<span class="comment">; Select audio/midi flags here according to platform</span>
<span class="comment">; Audio out   Audio in    No messages</span>
-odac           -iadc     -d     <span class="comment">;;;RT audio I/O</span>
<span class="comment">; For Non-realtime ouput leave only the line below:</span>
<span class="comment">; -o spat3d_quad.wav -W ;;; for file output any platform</span>
<span class="csdtag">&lt;/CsOptions&gt;</span>
<span class="csdtag">&lt;CsInstruments&gt;</span>

<span class="op">/</span><span class="op">*</span> Written by Istvan Varga <span class="op">*</span><span class="op">/</span>
<span class="ohdr">sr</span>      <span class="op">=</span>  48000
<span class="ohdr">kr</span>      <span class="op">=</span>  1000
<span class="ohdr">ksmps</span>   <span class="op">=</span>  48
<span class="ohdr">nchnls</span>  <span class="op">=</span>  4

<span class="op">/</span><span class="op">*</span> room parameters <span class="op">*</span><span class="op">/</span>

idep    <span class="op">=</span>  3    <span class="op">/</span><span class="op">*</span> early reflection depth       <span class="op">*</span><span class="op">/</span>

itmp    <span class="ohdr">ftgen</span>   1, 0, 64, <span class="op">-</span>2,                                           \
		<span class="op">/</span><span class="op">*</span> depth1, depth2, <span class="opc">max</span> delay, IR length, idist, <span class="ohdr">seed</span> <span class="op">*</span><span class="op">/</span> \
		idep, 48, <span class="op">-</span>1, 0.01, 0.25, 123,                          \
		1, 21.982, 0.05, 0.87, 4000.0, 0.6, 0.7, 2, <span class="op">/</span><span class="op">*</span> <span class="opc">ceil</span>  <span class="op">*</span><span class="op">/</span> \
		1,  1.753, 0.05, 0.87, 3500.0, 0.5, 0.7, 2, <span class="op">/</span><span class="op">*</span> <span class="opc">floor</span> <span class="op">*</span><span class="op">/</span> \
		1, 15.220, 0.05, 0.87, 5000.0, 0.8, 0.7, 2, <span class="op">/</span><span class="op">*</span> front <span class="op">*</span><span class="op">/</span> \
		1,  9.317, 0.05, 0.87, 5000.0, 0.8, 0.7, 2, <span class="op">/</span><span class="op">*</span> back  <span class="op">*</span><span class="op">/</span> \
		1, 17.545, 0.05, 0.87, 5000.0, 0.8, 0.7, 2, <span class="op">/</span><span class="op">*</span> right <span class="op">*</span><span class="op">/</span> \
		1, 12.156, 0.05, 0.87, 5000.0, 0.8, 0.7, 2  <span class="op">/</span><span class="op">*</span> left  <span class="op">*</span><span class="op">/</span>

	<span class="oblock">instr</span> 1

<span class="op">/</span><span class="op">*</span> some source signal <span class="op">*</span><span class="op">/</span>

a1      <span class="opc">phasor</span> 150              <span class="comment">; oscillator</span>
a1      <span class="opc">butterbp</span> a1, 500, 200   <span class="comment">; filter</span>
a1      <span class="op">=</span>  <span class="opc">taninv</span>(a1 <span class="op">*</span> 100)
a2      <span class="opc">phasor</span> 3                <span class="comment">; envelope</span>
a2      mirror 40<span class="op">*</span>a2, <span class="op">-</span>100, 5
a2      limit a2, 0, 1
a1      <span class="op">=</span>  a1 <span class="op">*</span> a2 <span class="op">*</span> 9000

kazim   <span class="opc">line</span> 0, 2.5, 360        <span class="comment">; move sound source around</span>
kdist   <span class="opc">line</span> 1, 10, 4           <span class="comment">; distance</span>

<span class="comment">; convert polar coordinates</span>
kX      <span class="op">=</span>  <span class="opc">sin</span>(kazim <span class="op">*</span> 3.14159 <span class="op">/</span> 180) <span class="op">*</span> kdist
kY      <span class="op">=</span>  <span class="opc">cos</span>(kazim <span class="op">*</span> 3.14159 <span class="op">/</span> 180) <span class="op">*</span> kdist
kZ      <span class="op">=</span>  0

a1      <span class="op">=</span>  a1 <span class="op">+</span> 0.000001 <span class="op">*</span> 0.000001     <span class="comment">; avoid underflows</span>

imode   <span class="op">=</span>  2    <span class="comment">; change this to 3 for 8 spk in a cube,</span>
		<span class="comment">; or 1 for simple stereo</span>

aW, aX, aY, aZ  <span class="opc">spat3d</span> a1, kX, kY, kZ, 1.0, 1, imode, 2, 2

aW      <span class="op">=</span>  aW <span class="op">*</span> 1.4142

<span class="comment">; stereo</span>
<span class="comment">;</span>
<span class="comment">;aL     =  aW + aY              /* left                 */</span>
<span class="comment">;aR     =  aW - aY              /* right                */</span>

<span class="comment">; quad (square)</span>
<span class="comment">;</span>
aFL     <span class="op">=</span>  aW <span class="op">+</span> aX <span class="op">+</span> aY         <span class="op">/</span><span class="op">*</span> front left           <span class="op">*</span><span class="op">/</span>
aFR     <span class="op">=</span>  aW <span class="op">+</span> aX <span class="op">-</span> aY         <span class="op">/</span><span class="op">*</span> front right          <span class="op">*</span><span class="op">/</span>
aRL     <span class="op">=</span>  aW <span class="op">-</span> aX <span class="op">+</span> aY         <span class="op">/</span><span class="op">*</span> rear left            <span class="op">*</span><span class="op">/</span>
aRR     <span class="op">=</span>  aW <span class="op">-</span> aX <span class="op">-</span> aY         <span class="op">/</span><span class="op">*</span> rear right           <span class="op">*</span><span class="op">/</span>

<span class="comment">; eight channels (cube)</span>
<span class="comment">;</span>
<span class="comment">;aUFL   =  aW + aX + aY + aZ    /* upper front left     */</span>
<span class="comment">;aUFR   =  aW + aX - aY + aZ    /* upper front right    */</span>
<span class="comment">;aURL   =  aW - aX + aY + aZ    /* upper rear left      */</span>
<span class="comment">;aURR   =  aW - aX - aY + aZ    /* upper rear right     */</span>
<span class="comment">;aLFL   =  aW + aX + aY - aZ    /* lower front left     */</span>
<span class="comment">;aLFR   =  aW + aX - aY - aZ    /* lower front right    */</span>
<span class="comment">;aLRL   =  aW - aX + aY - aZ    /* lower rear left      */</span>
<span class="comment">;aLRR   =  aW - aX - aY - aZ    /* lower rear right     */</span>

	<span class="opc">outq</span> aFL, aFR, aRL, aRR

	<span class="oblock">endin</span>


<span class="csdtag">&lt;/CsInstruments&gt;</span>
<span class="csdtag">&lt;CsScore&gt;</span>

/* Written by Istvan Varga */
<span class="stamnt">t</span> 0 60
<span class="stamnt">i</span> 1 0 10
<span class="stamnt">e</span>


<span class="csdtag">&lt;/CsScore&gt;</span>
<span class="csdtag">&lt;/CsoundSynthesizer&gt;</span>
</pre>
          </div>
        </div>
        <p><br class="example-break" />
    </p>
      </div>
      <div class="refsect1">
        <a id="idp150061664"></a>
        <h2>See Also</h2>
        <p>
      <a class="link" href="spat3di.html" title="spat3di"><em class="citetitle">spat3di</em></a>,
      <a class="link" href="spat3dt.html" title="spat3dt"><em class="citetitle">spat3dt</em></a>
    </p>
      </div>
      <div class="refsect1">
        <a id="idp150064736"></a>
        <h2>Credits</h2>
        <p>
      </p>
        <table border="0" summary="Simple list" class="simplelist">
          <tr>
            <td>Author: Istvan Varga</td>
          </tr>
          <tr>
            <td>2001</td>
          </tr>
        </table>
        <p>
    </p>
        <p>New in version 4.12</p>
        <p>Updated April 2002 by Istvan Varga</p>
      </div>
    </div>
    <div class="navfooter">
      <hr />
      <table width="100%" summary="Navigation footer">
        <tr>
          <td width="40%" align="left"><a accesskey="p" href="space.html">Prev</a> </td>
          <td width="20%" align="center">
            <a accesskey="u" href="OpcodesTop.html">Up</a>
          </td>
          <td width="40%" align="right"> <a accesskey="n" href="spat3di.html">Next</a></td>
        </tr>
        <tr>
          <td width="40%" align="left" valign="top">space </td>
          <td width="20%" align="center">
            <a accesskey="h" href="index.html">Home</a>
          </td>
          <td width="40%" align="right" valign="top"> spat3di</td>
        </tr>
      </table>
    </div>
  </body>
</html>
